const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const uglifyJs = require("uglifyjs-webpack-plugin"); //用来压缩js文件
const CleanWebpackPlugin = require("clean-webpack-plugin").CleanWebpackPlugin;

module.exports = {
  mode: "development",
  entry: "./index.js",
  output: {
    filename: "[name].[hash].js", //打包出来的入口js文件,只显示8位
    path: path.resolve(__dirname, "dist"), //打包出来的文件的路径
  },
  resolve: {
    extensions: [".js"],
    alias: {
      src: path.resolve(__dirname, "src"),
    },
  },
  devtool: "source-map",
  devServer: {
    //开发服务器配置
    port: 3000, //设置服务的端口号
    progress: false, //将运行进度输出到控制台。
    host: "127.0.0.1",
    contentBase: "./dist", //指定服务的默认目录
    compress: false, //是否压缩文件
    open: true, //是否自动打开浏览器
  },
  plugins: [
    new CleanWebpackPlugin(), //打包自动清空之前打包的文件目录
    new HtmlWebpackPlugin({
      filename: "index.html",
      template: "./public/index.html",
      //   chunks: ["common", "vendor", "mall"],
      hash: true, // 为静态资源生成hash值
      inject: "body",
      xhtml: true,
      minify: {
        removeComments: true,
      },
    }),
  ],
  optimization: {
    //优化项
    minimizer: [new uglifyJs()],
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            //使用babel-loader将es6转化为es5
            loader: "babel-loader",
            options: {
              presets: ["@babel/preset-env"],
              // plugins: [
              //     "syntax-dynamic-import",
              // ]
            },
          },
        ],
      },
      {
        test: /\.scss$/,
        use: ["style-loader", "css-loader", "sass-loader"],
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: "url-loader",
            options: {
              limit: 20000,
            },
          },
        ],
      },
    ],
  },
};
